<template>
	<view class="page tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
		<view class="top-bg"></view>
		<dawnBar title="我的申请"></dawnBar>
		<view class="page-container" style="padding-bottom: 140rpx;">
			<!-- 顶部标签 -->
			<!-- <view class="tn-bg-white dawn-shadow dawn-radius">
				<tn-tabs-swiper activeColor="#3165CC" inactiveColor="#080808" :list="tabList"
					name="label" :current="tabsIndex" @change="tabsChange"></tn-tabs-swiper>
			</view> -->

			<view class=" ">
				<!-- 标签内容 -->
				<view class="list">
					<view class="list-item" v-for="(item,index) in list" :key="index">
						<view class="tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
							<view @click="nav('/hosp/medicalcopy/detail?itemId='+item.id)"
								class="tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
								<text class=" tn-color-dawn">{{item.applyNo || '' }}</text>
								<!-- <text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text> -->
								<!-- <tn-tag class="dawn-tag-sm dawn-tag-sm__1">{{item.relation || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text> -->
							</view>
							<view>
								<view class="justify-content-item ">
									<StatusView type="medicalcopy" ui="tag" :status="item.status"></StatusView>
								</view>
							</view>
						</view>
						<view
							class="tn-padding-top-xs tn-padding-bottom-xs dawn-border-bottom tn-flex tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
							<view class="tn-flex tn-flex-nowrap  tn-flex-col-center tn-flex-row-left">
								<!-- <view @click="navLink('/hosp/medicalcopy/detail?itemId='+item.id)">
									<tn-avatar shape="square" :text="item.applicantName" size="130rpx"></tn-avatar>
								</view> -->
								<!-- <view class="tn-flex tn-flex-col-center">
									<view style="min-width: 80px;" class="dawn-radius tn-padding-sm tn-text-ellipsis tn-text-center"
										:class="item.status === 1?'tn-bg-dawn':'tn-bg-dawn-gray'">
										<view class="dawn-text-lg tn-text-bold">
											¥ {{item.totalPrice}}
										</view>
										<view class="tn-margin-top-sm" style="height: 20px;">
											金额
										</view>
									</view>
								</view> -->
								<view class="">
									<!-- <view class="tn-margin-top-xs">
										<text class="tn-icon-image-text-fill dawn-text-sm tn-margin-right-ss">申请人</text>
										<text class="dawn-text-sm">{{ item.patientName }}</text>
										<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
										<text class="tn-icon-ipad-fill dawn-text-sm tn-margin-right-ss"></text>
									</view> -->
									<view class="tn-flex tn-flex-col-center dawn-text-sm">
										<view>
											<text class="tn-margin-right-ss dawn-color-grey">就诊人</text>
											<text class="">{{ item.patientName }}</text>
										</view>
										<view v-if="item.relation !== '本人'">
											<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
											<text class="tn-margin-right-ss dawn-color-grey">申请人</text>
											<text class="">{{ item.applicantName }}</text>
										</view>


									</view>
									<view class="tn-margin-top-xs">
										<text class="dawn-text-sm tn-margin-right-ss dawn-color-grey">用途</text>
										<text class="dawn-text-sm">{{ item.purpose }}</text>
									</view>
									<view class="">
										<text class="dawn-text-sm tn-margin-right-ss dawn-color-grey">内容</text>
										<text class="dawn-text-sm">{{ item.content }}</text>
									</view>
								</view>
							</view>
							<view class="tn-flex tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
								<view class="justify-content-item">
									<text class="tn-margin-right-ss">¥ </text>
									<text
										class="tn-margin-right-xs dawn-text-lg tn-text-bold">{{item.totalPrice}}</text>
								</view>
							</view>
						</view>

						<view
							class="tn-padding-top-xs tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
							<view class="tn-flex tn-flex-col-center">
								<tn-tag class="dawn-tag-sm dawn-tag-sm__1">{{item.relation || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
								<tn-tag class="dawn-tag-sm dawn-tag-sm__2">{{item.collectType || '' }}</tn-tag>
							</view>
							<view
								class="tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-right op-box">
								<tn-button shadow v-if="item.status === 0 || item.status === 2"
									@click="nav('/hosp/medicalcopy/form?itemId='+item.id)" size="sm"
									class="dawn-btn-sm dawn-btn-sm__info">
									<text class="tn-icon-edit-write-fill">编辑</text>
								</tn-button>
								<tn-button shadow v-if="item.status === 1 || item.status === 3"
									@click="toOrderInfo(item)" size="sm" class="dawn-btn-sm dawn-btn-sm__pay">
									<text class="tn-icon-money-fill">支付</text>
								</tn-button>
								<tn-button @click="nav('/hosp/medicalcopy/detail?itemId='+item.id)" size="sm"
									class="dawn-btn-sm dawn-btn-sm__primary">
									<text class="tn-icon-send-fill">详情</text>
								</tn-button>
							</view>
						</view>
					</view>
				</view>

				<!-- 悬浮按钮-->
				<view class="tn-flex tn-footerfixed">
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button @click="getPage()" class="dawn-btn-lg dawn-btn-lg__main"
							backgroundColor="tn-main-gradient-dawn">
							<text class="tn-icon-search-list-fill tn-padding-right-xs"></text>
							<text>查 询</text>
						</tn-button>
					</view>
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button @click="nav('/hosp/medicalcopy/form?toAuth=1')" class="dawn-btn-lg dawn-btn-lg__info"
							style="width: 85% !important;">
							<text class="tn-icon-add-square-fill tn-padding-right-xs"></text>
							<text>新 增</text>
						</tn-button>
					</view>
				</view>
			</view>
			<view v-if="show">
				<dawnMsg :box="dawnMsgBox" :msg="msg"></dawnMsg>
			</view>
		</view>

		<!-- loading -->
		<dawnLoading :show="loading"></dawnLoading>

		<!-- 分页 -->
		<!-- <dawn-pagination ref="pagination" :current="queryData.pageNo" :total="total" @pagination="getPage" /> -->

		<!-- 悬浮按钮-->
		<NavBtn></NavBtn>
	</view>
</template>

<script>
	import page_mixin from '@/mixins/page_mixin.js'
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import * as MedicalCopyApi from '@/api/hospital/medicalcopy.js';
	import * as api from '@/api/api.js';
	import * as config from "@/api/config.js";
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	const app = getApp();

	export default {
		name: 'order',
		mixins: [page_mixin],
		computed: mapGetters(['appConfig', 'uid', 'isLogin', 'userInfo', 'patientInfo']),
		data() {
			return {
				loading: false,
				show: false,
				msg: '',
				dawnMsgBox: '',
				queryData: {
					pageSize: 10,
					pageNo: 1,
				},
				tabsIndex: 0,
				tabList: [{
					value: '0',
					label: '待审核'
				}, {
					value: '1',
					label: '待支付'
				}, {
					value: '0',
					label: '已完成'
				}, {
					value: '-1',
					label: '全部'
				}],
				list: []
			}
		},
		onLoad(options) {
			if (!this.isLogin) {
				toLogin();
				return;
			} else {
				this.getPage();
			}
		},
		methods: {
			// 标签栏值发生改变
			tabsChange(index) {
				this.tabsIndex = index;
				console.table(this.tabList[index]);
				this.queryData.status = this.tabList[index].value;
				this.getPage();
			},
			async getPage(page) {
				let that = this;
				this.loading = true;
				this.show = false;
				this.dawnMsgBox = '';

				this.queryData.userId = this.userInfo.id;
				this.queryData.openid = this.userInfo.openid;
				// this.queryData.patientId = this.patientInfo.patientId;
				// this.queryData.patientName = this.patientInfo.name;
				MedicalCopyApi.getMedicalCopyPage(this.queryData).then(res => {
					this.loading = false;
					console.table(res);
					if (res.code === 0) {
						const l = res.data.list || [];
						if (l.length == 0) {
							that.show = true;
							that.msg = '暂无数据';
						} else {
							that.$set(that, 'list', l);
						}
					} else {
						that.show = true;
						that.msg = res.msg;
					}
				}).catch(err => {
					this.loading = false;
					this.show = true;
				})
			},
			toOrderInfo(item) {
				// uni.navigateTo({
				// 	url: '/hosp/order/info?orderNumber=' + item.applyNo
				// })
				this.toPayUI(item.applyNo);
			},
			navDel(item) {
				let that = this;
				uni.showModal({
					title: '系统提示',
					content: '请确认是否要解绑?',
					success: function(res) {
						if (res.confirm) {
							that.navDelConfirm(item);
						} else if (res.cancel) {
							// console.log('取消');
							//TODO
						}
					}
				});
			},
			navDelConfirm(item) {
				let that = this;
				this.loading = true;
				this.show = false;
				this.dawnMsgBox = 'popup';
				const cancelData = new Object();
				cancelData.patientId = item.patientId;
				cancelData.patientName = item.name;
				cancelData.id = item.id;
				PatientApi.del(cancelData).then(res => {
					that.loading = false;
					console.table(res);
					if (res.code === 0) {
						that.$util.Tips({
							title: '解绑档案成功'
						});
						that.getPage();
					} else {
						that.show = true;
						that.msg = res.msg;
					}
				}).catch(err => {
					that.loading = false;
					that.show = true;
					that.msg = err.message;
				})
			},
			formatDate: function(date) {
				return dayjs(date).format("YYYY-MM-DD");
			},
			formatDateTime: function(date) {
				return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tn-avatar {
		z-index: 0 !important;
	}
</style>